<?php
    header("Content-type: text/html; charset=utf-8");
        $username = $_POST['username'];
        $password = $_POST['password'];
        $repassword = $_POST['repassword'];
        $conn = new mysqli('localhost','root','root');
        //创建数据库test
$sql1 = "CREATE DATABASE test";
if ($conn->query($sql1) === TRUE) {
    echo "首次访问，数据库创建成功！！！！";
} else {
    
}
//选择数据库
mysqli_select_db($conn,"test");
$sql1 = "CREATE TABLE user (
    id INT(6)  AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(50) NOT NULL,
    password VARCHAR(50) NOT NULL
    )";
     
    if ($conn->query($sql1) === TRUE) {
        echo "首次访问，创建user表成功！！！！";
    } 



        if ($username == ''){
            echo '<script>alert("请输入用户名！");history.go(-1);</script>';
            exit(0);
        }
        if ($password == ''){
            echo '<script>alert("请输入密码");history.go(-1);</script>';
            exit(0);
        }
        if ($repassword !=$password){
            echo '<script>alert("两次输入的密码不一致，重新输入！");history.go(-1);</script>';
            exit(0);
        }
        if ($repassword ==$password){
            $conn = new mysqli('localhost','root','root','test');
            if ($conn->connect_error){
                echo '数据库连接失败！';
                exit(0);
            }else {
                $sql = "select username from user where username = '$_POST[username]'";
                $result = $conn->query($sql);
                $number = mysqli_num_rows($result);
                if ($number) {
                    echo '<script>alert("用户名已经存在");history.go(-1);</script>';
                } else {
                    $sql_insert = "insert into user (username,password) values('$_POST[username]','$_POST[password]')";
                    $res_insert = $conn->query($sql_insert);
                    if ($res_insert) {
						echo '<script>alert("注册成功,关闭进入登录！！！");</script>';
                        echo '<script>window.location="../login.html";</script>';
                    } else {
                        echo "<script>alert('注册失败！！！');history.go(-1);</script>";
                    }
                }
            }
        }
?>